package com.rjkj.medicalwaste.ui.xlb

import android.util.Log
import androidx.lifecycle.viewModelScope
import com.rjkj.medicalwaste.base.BaseViewModel
import com.rjkj.medicalwaste.bean.OCRBean
import com.rjkj.medicalwaste.network.response.catchException
import com.rjkj.medicalwaste.network.response.next
import com.rjkj.medicalwaste.repository.GarbageRepository
import com.rjkj.medicalwaste.repository.UserRepository
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class XlbTestViewModel(
    private val repository: UserRepository,
    private val repository2: GarbageRepository
) : BaseViewModel() {
    fun getAllUserData(serialNum: String) {
        viewModelScope.launch {
            showLoading()
            request(false) {
                getAllUser(deviceSN = serialNum)
            }.next {
                Log.e("TAG", "testRequestData: ${this.data.toString()}")
                this.data?.user?.let {
                    repository.update(it)
                }.also {
                    Log.e(TAG, "getAllUserData: $it")
                    tips.value = "已成功拉取数据${it}条"
                }
                closeLoading()
            }.catchException {
                printStackTrace()
                doException(this)
            }
        }
    }

    fun deleteAllGarbageData() {
        viewModelScope.launch(Dispatchers.IO) {
            repository2.deleteAllGarbage()
            tips.postValue("已删除数据")
        }
    }

    fun testAPI() {
        viewModelScope.launch {
            request(false) {
                test(
                    req = OCRBean(
                        url = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fbpic.588ku.com%2Felement_origin_min_pic%2F16%2F10%2F29%2F2ac8e99273bc079e40a8dc079ca11b1f.jpg&refer=http%3A%2F%2Fbpic.588ku.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1628408675&t=1c31c836ee24b7f5c2ee212b35569d1e",
                        prob = false,
                        charInfo = false,
                        rotate = false,
                        table = false,
                        sortPage = false,
                        img = ""
                    )
                )
            }.next {
                Log.e("TAG", "testRequestData: ${this.data.toString()}")
            }.catchException {
                printStackTrace()
                doException(this)
            }
        }
    }

}